package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.config.VodConstantProperties;
import com.atguigu.tingshu.album.mapper.AlbumInfoMapper;
import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.album.mapper.TrackStatMapper;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.album.TrackStat;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoServiceImpl extends ServiceImpl<TrackInfoMapper, TrackInfo> implements TrackInfoService {

    @Autowired
    private TrackInfoMapper trackInfoMapper;
    @Resource
    VodConstantProperties vodConstantProperties;

    @Resource
    VodService vodService;
    @Resource
    TrackStatMapper statMapper;
    @Resource
    AlbumInfoMapper albumInfoMapper;


    @Override
    public void saveTrackInfo(TrackInfoVo trackInfoVo) {

        //1 根据文件id去腾讯云中查文件信息
        TrackMediaInfoVo mediaInfo = this.vodService.getMediaInfo(trackInfoVo.getMediaFileId());
        //2 保存声音信息
        TrackInfo trackInfo = new TrackInfo();
        BeanUtils.copyProperties(trackInfoVo, trackInfo);
        // !!!
        Long userId = AuthContextHolder.getUserId();
        userId = userId != null ? userId : 1;
        trackInfo.setUserId(userId);
//设置order_num : 先查询用一个专辑下最大的声音编号，在进行加 1
        TrackInfo trackInfo1 = this.getOne(new LambdaQueryWrapper<>(TrackInfo.class)
                .eq(TrackInfo::getAlbumId, trackInfoVo.getAlbumId())
                .orderByDesc(TrackInfo::getOrderNum).select(TrackInfo::getOrderNum).last("limit 1"));
        // /如果前一个声音为空 说明当前声音是上传的第一个 否则 就在当前的序列号上进行加1
        trackInfo.setOrderNum(trackInfo1 == null ? 1 : trackInfo1.getOrderNum() + 1);

        trackInfo.setMediaSize(mediaInfo.getSize());
        trackInfo.setMediaType(mediaInfo.getType());
        trackInfo.setMediaDuration(new BigDecimal(mediaInfo.getDuration()));

        trackInfo.setMediaUrl(mediaInfo.getMediakUrl());
        trackInfo.setStatus(SystemConstant.ALBUM_STATUS_PASS);
        this.save(trackInfo);
        Long trackId = trackInfo.getId();
        //3 保存声音的统计信息
        trackStatInfo(trackId, SystemConstant.TRACK_STAT_PLAY);
        trackStatInfo(trackId, SystemConstant.TRACK_STAT_COLLECT);
        trackStatInfo(trackId, SystemConstant.TRACK_STAT_PRAISE);
        trackStatInfo(trackId, SystemConstant.TRACK_STAT_COMMENT);
        //4 更新专辑中的包含音乐数

        AlbumInfo albumInfo = this.albumInfoMapper.selectById(trackInfoVo.getAlbumId());
        albumInfo.setIncludeTrackCount(albumInfo.getIncludeTrackCount() + 1);
        this.albumInfoMapper.updateById(albumInfo);

    }

    @Override
    public Page<TrackListVo> findUserTrackPage(Integer page, Integer limit, TrackInfoQuery query) {

        // 1 声音信息表
        query.setUserId(AuthContextHolder.getUserId() == null ? 1 : AuthContextHolder.getUserId());
        return trackInfoMapper.findUserTrackPage(new Page<>(page, limit), query);


        // 2 声音统计表


    }

    @Override
    public void removeTrackInfo(Long trackId) {

        // 1 根据id 删除 声音信息表
        TrackInfo trackInfo = this.getById(trackId);
        this.trackInfoMapper.deleteById(trackId);
        // 2 删除 声音统计表
        statMapper.delete(new LambdaUpdateWrapper<TrackStat>().eq(TrackStat::getTrackId, trackId));
        // 3 更新专辑表中声音的数量 -1

        AlbumInfo albumInfo = albumInfoMapper.selectById(trackInfo.getAlbumId());
        albumInfo.setIncludeTrackCount(albumInfo.getIncludeTrackCount() - 1);
        this.albumInfoMapper.updateById(albumInfo);

        //删除腾讯云的音频文件
        vodService.removeMediaFile(trackInfo.getMediaFileId());
    }

    @Override
    public void updateTrackInfo(Long trackId, TrackInfoVo trackInfoVo) {
        TrackMediaInfoVo mediaInfo = vodService.getMediaInfo(trackInfoVo.getMediaFileId());
        TrackInfo trackInfo = new TrackInfo();
        //更新声音表
        BeanUtils.copyProperties(trackInfoVo, trackInfo);
        trackInfo.setId(trackId);
        // 设置媒体信息
        trackInfo.setMediaDuration(new BigDecimal(mediaInfo.getDuration()));
        trackInfo.setMediaType(mediaInfo.getType());
        trackInfo.setMediaSize(mediaInfo.getSize());
        trackInfo.setMediaUrl(mediaInfo.getMediakUrl());
        this.updateById(trackInfo);

    }


    private void trackStatInfo(Long trackId, String type) {
        TrackStat trackStat = new TrackStat();
        trackStat.setTrackId(trackId);
        trackStat.setStatNum(0);
        trackStat.setStatType(type);
        this.statMapper.insert(trackStat);
    }


}
